package com.sensePlusVR.bean;

import java.util.List;

import org.xutils.DbManager;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
import org.xutils.ex.DbException;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.utils.MyLog;

@Table(name = "FeedbackReplyItem")
public class FeedbackReplyItem implements Parcelable{
	private static final String TAG = FeedbackReplyItem.class.getSimpleName();
	@Column(name = "replyId",isId = true,autoGen = false)
	private Integer replyId;
	
	@Column(name = "feedbackId")
	private Integer feedbackId;

	@Column(name = "content")
	private String content;

	@Column(name = "type")
	private Byte type;
	
	@Column(name = "showUrls")
	private String showUrls;
	
	@Column(name = "isRead")
	private Byte isRead;
	
	@Column(name = "createTime")
	private String createTime;
	

	public FeedbackReplyItem(){}
	
	public FeedbackReplyItem(Parcel in){
		replyId = in.readInt();
		feedbackId = in.readInt();
		content = in.readString();
		type = in.readByte();
		showUrls = in.readString();
		isRead = in.readByte();
		createTime = in.readString();
	}
	
	public Integer getReplyId() {
		return replyId;
	}

	public void setReplyId(Integer replyId) {
		this.replyId = replyId;
	}

	
	public Integer getFeedbackId() {
		return feedbackId;
	}

	public void setFeedbackId(Integer feedbackId) {
		this.feedbackId = feedbackId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Byte getType() {
		return type;
	}

	public void setType(Byte type) {
		this.type = type;
	}

	public String getShowUrls() {
		return showUrls;
	}

	public void setShowUrls(String showUrls) {
		this.showUrls = showUrls;
	}

	public Byte getIsRead() {
		return isRead;
	}

	public void setIsRead(Byte isRead) {
		this.isRead = isRead;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	@Override
	public String toString() {
		return "FeedbackReplyItem [replyId=" + replyId + ", feedbackId="
				+ feedbackId + ", content=" + content + ", type=" + type
				+ ", showUrls=" + showUrls + ", isRead=" + isRead
				+ ", createTime=" + createTime + "]";
	}
	
	public static void saveOrUpdate(Object object) {
		try {
    		Context context = VRApplication.getAppContext();
    		if(context != null) {
    			DbManager db = VRApplication.getDbManager();
    			db.saveOrUpdate(object);
    		}
    	}catch(Exception e){
    		MyLog.e(TAG, "saveOrUpdate failed: "+e);
    	}
	}
	
	public static List<FeedbackReplyItem> getFeedbackReply(Integer feedbackId){
		Context context = VRApplication.getAppContext();
		if(context != null) {
			DbManager db = VRApplication.getDbManager();
			try {
				return db.selector(FeedbackReplyItem.class).where("feedbackId", "=", feedbackId).findAll();
			} catch (DbException e) {
				MyLog.e(TAG, "getFeedbackReply failed: "+e);
				return null;
			}
		}
		return null;
		
	}
	
	public static List<FeedbackReplyItem> getAllFeedbackReply(){
		
		Context context = VRApplication.getAppContext();
		if(context != null){
			
			DbManager db = VRApplication.getDbManager();
			
			try {
				return db.selector(FeedbackReplyItem.class).orderBy("createTime", true).findAll();
			} catch (DbException e) {
				MyLog.e(TAG, "getAllFeedbackReply failed: "+e);
				return null;
			}
		}
		return null;
	}
	
	public static int getAllFeedbackReplyCount(){
		
		List<FeedbackReplyItem> feedbackReplyItems = getAllFeedbackReply();
		if(feedbackReplyItems != null){
			
			return feedbackReplyItems.size();
			
		}
		
		return 0;
		
	}
	

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(replyId);
		dest.writeInt(feedbackId);
		dest.writeString(content);
		dest.writeByte(type);
		dest.writeString(showUrls);
		dest.writeByte(isRead);
		dest.writeString(createTime);
	}
	
	public static final Parcelable.Creator<FeedbackReplyItem> CREATOR = new Creator<FeedbackReplyItem>() {
		
		@Override
		public FeedbackReplyItem[] newArray(int size) {
			return new FeedbackReplyItem[size];
		}
		
		@Override
		public FeedbackReplyItem createFromParcel(Parcel in) {
			return new FeedbackReplyItem(in);
		}
	};
}
